#include "lrcpage.h"
#include "ui_lrcpage.h"
#include<QPushButton>
#include<QFile>
#include<QDebug>

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    lrcAnimation=new QPropertyAnimation(this,"geometry",this);
    lrcAnimation->setDuration(500);
    lrcAnimation->setStartValue(QRect(9,9,width(),height()));
    lrcAnimation->setEndValue(QRect(9,9+height(),width(),height()));
    connect(ui->hideBtn,&QPushButton::clicked,this,[=]{
        lrcAnimation->start();
        //this->hide();动画结束隐藏
    });
    connect(lrcAnimation,&QPropertyAnimation::finished,this,[=]{
        this->hide();
    });
    ui->hideBtn->setIcon(QIcon(":/image/hide.png"));
}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrc(QString lrcPath)
{
    lrcLines.clear();//清除上一首的记录
    QFile lrcFile(lrcPath);
    if(!lrcFile.open(QFile::ReadOnly))
    {
        qDebug()<<"读取文件失败";
        return false;
    }
    while(!lrcFile.atEnd())
    {
        QString lrcLine=lrcFile.readLine(1024);

        int left=0,right=0;
        left=lrcLine.indexOf('[');
        right=lrcLine.indexOf(']');

        QString time=lrcLine.mid(left,right-left+1);
        qint64 lineTime=0;
        int begin=1,end=0;

        end=time.indexOf(":");
        lineTime+=time.mid(begin,end-begin).toInt()*60*1000;

        begin=end+1;
        end=time.indexOf('.',begin);
        lineTime+=time.mid(begin,end-begin).toInt()*1000;

        begin=end+1;
        end=time.indexOf('.',begin);
        lineTime+=time.mid(begin,end-begin).toInt();

        QString lineText=lrcLine.mid(right+1,lrcLine.size()-right-1-1-1);//多-1是因为换行
        lrcLines.push_back(LrcLine(lineTime,lineText));
    }
    for(auto word : lrcLines)
    {
        qDebug()<<word.lrcTime<<" "<<word.lrcText;
    }
    return true;
}

int LrcPage::getLineLrcWordIndex(qint64 pos)
{
    if(lrcLines.isEmpty())
    {
        return -1;
    }
    if(pos<lrcLines[0].lrcTime)
    {
        return 0;
    }
    for(int i=1;i<lrcLines.size();i++)
    {
        if(pos>lrcLines[i-1].lrcTime&&pos<=lrcLines[i].lrcTime)
        return i-1;
    }
    return lrcLines.size()-1;
}

QString LrcPage::getLineLrcWord(int index)
{
    if(index<0||index>=lrcLines.size())
    {
        return "";
    }
    return lrcLines[index].lrcText;
}

void LrcPage::ShowLineLrcWord(int time)
{
    int index=getLineLrcWordIndex(time);
    if(index==-1)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("当前歌曲无歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(getLineLrcWord(index-3));
        ui->line2->setText(getLineLrcWord(index-2));
        ui->line3->setText(getLineLrcWord(index-1));
        ui->lineCenter->setText(getLineLrcWord(index));
        ui->line4->setText(getLineLrcWord(index+1));
        ui->line5->setText(getLineLrcWord(index+2));
        ui->line6->setText(getLineLrcWord(index+3));
    }
}

void LrcPage::ShowLrcMusicName(QString name)
{
    ui->musicName->setText(name);
}

void LrcPage::ShowLrcMusicSinger(QString singer)
{
    ui->musicSinger->setText(singer);
}
